package com.gxkj.projects.myshopx.dao;

import com.gxkj.common.hibernate.HibernateBaseRepositoryImpl;
import com.gxkj.common.utils.ListPager;
import com.gxkj.projects.myshopx.entitys.GoodType;
import com.gxkj.projects.myshopx.entitys.User;
import com.gxkj.projects.myshopx.enums.GoodTypeStates;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

@Repository
public class GoodTypeDaoImpl  extends HibernateBaseRepositoryImpl {

    public ListPager<GoodType> doPageHQL(int pagenNo, int pageSize, GoodType entity ){
        ListPager<GoodType> pager = new ListPager<GoodType>();
        pager.setPageNo(pagenNo);
        pager.setRowsPerPage(pageSize);
        Map<String,Object> param = new HashMap<String,Object>();
        String hql  = " from GoodType g   ";
        if(entity == null || entity.getStates() == null){
            hql += " where states != :states";
            param.put("states", GoodTypeStates.DEL);
        }else {
            hql += " where  1 = 1";
        }

        if(entity != null) {
            if(StringUtils.isNoneBlank(entity.getName())){
                hql += " and name like :name";
                param.put("name","%"+entity.getName()+"%");
            }
            if(entity.getStates() != null){
                hql += " and states = :states";
                param.put("states", entity.getStates());
            }
        }
        hql += " order by createdAt desc";
        return  this.selectPageByHql(hql,param,pager);
    }
}
